package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.ConfSupplier;
import com.jic.point.model.entity.ConfSupplierApply;
import com.jic.point.model.vo.request.ConfSupplierApplyRequest;
import com.jic.point.model.vo.response.ConfSupplierApplyResponse;
import com.jic.point.service.ConfSupplierApplyService;
import com.jic.point.service.ConfSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author: admin
 * @date: 2020-11-26 16:54:02
 */
 @RestController
 @Slf4j
 @RequestMapping("/confSupplierApply")
 @Api(tags = " [供应商申请表]控制类-PC")
public class ConfSupplierApplyController {
	@Autowired
	private ConfSupplierApplyService service;
	@Resource
    ConfSupplierService confSupplierService;
	
	
     /**
      *新增记录
      * @param request 新增[供应商申请表]请求参数
      * @return ConfSupplierApplyResponse 新增[供应商申请表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[供应商申请表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfSupplierApplyRequest request) throws Throwable {
     		ConfSupplierApply confSupplierApply = new ConfSupplierApply();
    		BeanUtils.copyProperties(request, confSupplierApply);
    	    long flag = service.save(confSupplierApply);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      * @param request 新增或修改[供应商申请表]请求参数
      * @return ConfSupplierApplyResponse 新增或修改[供应商申请表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[供应商申请表]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfSupplierApplyRequest request) throws Throwable {
      		ConfSupplierApply confSupplierApply = new ConfSupplierApply();
		     BeanUtils.copyProperties(request, confSupplierApply);
	         long flag =   service.saveOrUpdate(confSupplierApply);
      return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[供应商申请表]请求参数
      * @return ConfSupplierApplyResponse 删除[供应商申请表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[供应商申请表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody ConfSupplierApplyRequest request) throws Throwable {
      		ConfSupplierApply confSupplierApply = new ConfSupplierApply();
	     	BeanUtils.copyProperties(request, confSupplierApply);
	         long flag = service.deleteByPrimaryKeyLogically(confSupplierApply);
      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[供应商申请表]请求参数
      * @return ConfSupplierApplyResponse 修改[供应商申请表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[供应商申请表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody ConfSupplierApplyRequest request) throws Throwable {
      		ConfSupplierApply confSupplierApply = new ConfSupplierApply();
	     	BeanUtils.copyProperties(request, confSupplierApply);
	         long flag = service.updateByPrimaryKeySelective(confSupplierApply);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[供应商申请表]详情请求参数
      * @return ConfSupplierApplyResponse 查询[供应商申请表]详情响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[供应商申请表]详情")
      @ResponseBody
     public RestResult<ConfSupplierApplyResponse> getByPrimaryKey(@Validated  @RequestBody ConfSupplierApplyRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	ConfSupplierApply confSupplierApply = new ConfSupplierApply();
	     	BeanUtils.copyProperties(request, confSupplierApply);
	     	ConfSupplierApply result = service.getByPrimaryKey(confSupplierApply);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode
                        .EXCEPTION, "记录不存在");
		     }
		     ConfSupplierApplyResponse bo = new ConfSupplierApplyResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new ConfSupplierApplyResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<ConfSupplierApply>> listPage (@RequestBody ConfSupplierApplyRequest request) throws Throwable {
      ConfSupplierApply param = new ConfSupplierApply();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *CONF_SUPPLIER_APPLY 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [供应商申请表] ")
      @ResponseBody
      public RestResult<List<ConfSupplierApplyResponse>>  listConfSupplierApplyAllRecord(@RequestBody ConfSupplierApplyRequest request) throws Throwable {
      		ConfSupplierApply param = new ConfSupplierApply();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<ConfSupplierApply> list = service.listAllRecord(param);
			List<ConfSupplierApplyResponse> responseList = list.stream().map(temp -> {
			ConfSupplierApplyResponse bo = new ConfSupplierApplyResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }

    /**
     * 审核提交
     * @param request
     * @return
     */
    @PostMapping("/auditAubmit")
    @ApiOperation(value = "审核提交 [第三方机构主表] ")
    @ResponseBody
    public RestResult<Long> auditAubmit(@Validated @RequestBody ConfSupplierApplyRequest request){
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        ConfSupplierApply confSupplierApply = new ConfSupplierApply();
        BeanUtils.copyProperties(request, confSupplierApply);
        ConfSupplierApply result =  service.getByPrimaryKey(confSupplierApply);
        //赋值
        result.setAuditSts(request.getAuditSts());
        result.setAuditReason(request.getAuditReason());
        result.setAuditorId(request.getAdminOperate().getOperator());
        //审核成功
        if(result.getAuditSts() == 1){
            //审核成功------增加
            if(result.getCurrentAction() == 0 ){
                //申请表逻辑删除
                service.deleteByPrimaryKeyLogically(result);
                //插入到主表中
                ConfSupplier confSupplier = new ConfSupplier();
                BeanUtils.copyProperties(result, confSupplier);
                confSupplier.setId(null);
                confSupplier.setDeleteFlag(0);
                long flag = confSupplierService.insertSelective(confSupplier);
                return RestResult.success(flag);
            }else  if(result.getCurrentAction() == 1 ){
                //修改成功
                //申请表逻辑删除
                service.deleteByPrimaryKeyLogically(result);
                //插入到主表--------通过code修改主表delete_flag 状态 0
                ConfSupplier confSupplier = new ConfSupplier();
                BeanUtils.copyProperties(result, confSupplier);
                confSupplier.setDeleteFlag(0);
                long flag = confSupplierService.saveOrUpdateApply(confSupplier);
                return RestResult.success(flag);
            }else {
                //删除成功
                long flag = service.deleteByPrimaryKeyLogically(result);
                return RestResult.success(flag);
            }
        }else if(result.getAuditSts() == 2){
            //审核失败 ---------增加
            if(result.getCurrentAction() == 0 ){
                //申请表逻辑删除
                long flag = service.deleteByPrimaryKeyLogically(result);
                return RestResult.success(flag);
            }else {
                //审核失败-----修改 删除------申请表逻辑删除
                service.deleteByPrimaryKeyLogically(result);
                //插入到主表--------通过code修改主表delete_flag 状态 0
                ConfSupplier confSupplier = new ConfSupplier();
                BeanUtils.copyProperties(result, confSupplier);
                confSupplier.setDeleteFlag(0);
                long flag = confSupplierService.saveOrUpdateApply(confSupplier);
                return RestResult.success(flag);

            }
        }else{//审核驳回    只修改状态
            confSupplierApply.setAuditSts(3);
            int flag = service.updateByPrimaryKeySelective(confSupplierApply);
            return RestResult.success((long)flag);
        }
    }
 }

	
